import 'package:flutter/material.dart';
import 'package:ichinesereader/models/class/announcementsResult.dart';
import 'package:ichinesereader/models/teacher/TeacherClass.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

// ignore: must_be_immutable
class StudentAnnouncementsDataGrids extends StatelessWidget {
  StudentAnnouncementsDataGrids(this.teacherClass, this.announcementItems);

  TeacherClass teacherClass;
  List<Announcement> announcementItems;
  bool isWebOrDesktop = GetPlatform.isMacOS || GetPlatform.isWeb;
  List<DataGridRow> dataGridRows = <DataGridRow>[];

  DataGridController _dataGridController = DataGridController();

  @override
  Widget build(BuildContext context) {
    Log.debug('build student Announcements DataGrids');

    return Column(children: [
      Expanded(child: _buildDataGrid()),
    ]);
  }

  SfDataGrid _buildDataGrid() {
    return SfDataGrid(
      source: AnnouncementsDataGridSource(items: announcementItems),
      onQueryRowHeight: (row) {
        return row.rowIndex == 0 ? 50.0 : 49.0;
      },
      //设置表头高度
      showCheckboxColumn: false,
      allowSorting: true,
      allowTriStateSorting: true,
      allowSwiping: false,
      controller: _dataGridController,
      selectionMode: SelectionMode.single,
      navigationMode: GridNavigationMode.row,
      columns: <GridColumn>[
        GridColumn(
          width: 200,
          columnName: 'Type',
          label: buildDataGridColumnLabel('Type'),
        ),
        GridColumn(
          columnName: 'Reward',
          width: isWebOrDesktop ? 120 : 100,
          label: buildDataGridColumnLabel('Reward'),
        ),
        GridColumn(
          columnName: 'Message',
          width: isWebOrDesktop ? 480.0 : 280.0,
          visible: true,
          label: buildDataGridColumnLabel('Message'),
        ),
        GridColumn(
          columnName: 'CreateDate',
          width: 140,
          label: buildDataGridColumnLabel('CreateDate'),
        ),
        GridColumn(
          columnName: 'Id',
          width: 5,
          visible: false,
          label: buildDataGridColumnLabel('Id'),
        ),
      ],
    );
  }
}

class AnnouncementsDataGridSource extends DataGridSource {
  AnnouncementsDataGridSource({required List<Announcement> items}) {
    dataGridRows = items
        .map<DataGridRow>((dataGridRow) => DataGridRow(cells: [
              DataGridCell<String>(
                  columnName: 'Type', value: dataGridRow.announcementType),
              DataGridCell<int>(columnName: 'Reward', value: dataGridRow.point),
              DataGridCell<String>(
                  columnName: 'Message', value: dataGridRow.message),
              DataGridCell<int>(
                columnName: 'CreateDate',
                value: dataGridRow.when,
              ),
              DataGridCell<String>(columnName: 'Id', value: dataGridRow.id),
            ]))
        .toList();
  }

  List<DataGridRow> dataGridRows = [];

  @override
  List<DataGridRow> get rows => dataGridRows;

  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    //列表隔行Row的颜色 // https://help.syncfusion.com/flutter/datagrid/conditional-styling
    Color setRowBackgroundColor() {
      final int index = effectiveRows.indexOf(row);
      if (index % 2 != 0) {
        return Get.isDarkMode ? Colors.black : Colors.grey[200]!;
      }
      return Colors.transparent;
    }

    return DataGridRowAdapter(
        color: setRowBackgroundColor(),
        cells: row.getCells().map<Widget>((dataGridCell) {
          if (dataGridCell.columnName == 'CreateDate') {
            if (dataGridCell.value != null && dataGridCell.value > 0) {
              return Container(
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.symmetric(horizontal: 16.0),
                  child: Text(
                    DateFormat('yyyy-MM-dd').format(
                        DateTime.fromMillisecondsSinceEpoch(
                            dataGridCell.value)),
                    overflow: TextOverflow.ellipsis,
                    style: Get.textTheme.bodySmall,
                  ));
            } else {
              return Container(
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.symmetric(horizontal: 16.0),
                  child: Text(
                    '',
                    overflow: TextOverflow.ellipsis,
                    style: Get.textTheme.bodySmall,
                  ));
            }
          } else {
            return Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.symmetric(horizontal: 16.0),
                child: Text(
                  dataGridCell.value != null
                      ? dataGridCell.value.toString()
                      : '',
                  softWrap: true,
                  maxLines: 3,
                  overflow: TextOverflow.ellipsis,
                  style: Get.textTheme.bodySmall,
                ));
          }
        }).toList());
  }
}
